package client.popups.wizard;

import javax.swing.DefaultListModel;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTextField;
import javax.swing.JList;
import javax.swing.ListSelectionModel;
import javax.swing.JScrollPane;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import data.room.Room;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import miscellaneous.Storage;
import mockup.Mockup;
import mockup.MockupToData;
import mockup.ReservationMockup;
import mockup.RoomMockup;
import javax.swing.LayoutStyle.ComponentPlacement;

public class AppointmentsRoomDescription extends JPanel{
	private JTextField capacityTextField;
	private JList roomJList;
	private DefaultListModel model;
	private List<Room> roomList;

	public AppointmentsRoomDescription() {
		model = new DefaultListModel();
		roomList = new ArrayList<Room>();
		
		JLabel lblCapacity = new JLabel("Minimum kapasitet:");

		capacityTextField = new JTextField();
		capacityTextField.setColumns(10);
		capacityTextField.addKeyListener(new TextFieldKeyListener());


		roomJList = new JList(model);
		roomJList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setViewportView(roomJList);
		
		JLabel lblLedigeRom = new JLabel("Ledige rom:");
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(22)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 211, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblLedigeRom)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(lblCapacity)
							.addGap(4)
							.addComponent(capacityTextField, GroupLayout.PREFERRED_SIZE, 49, GroupLayout.PREFERRED_SIZE)))
					.addGap(217))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(11)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(3)
							.addComponent(lblCapacity))
						.addComponent(capacityTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(lblLedigeRom)
					.addGap(18)
					.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 294, Short.MAX_VALUE)
					.addContainerGap())
		);
		setLayout(groupLayout);
	}

	public JTextField getCapacityTextField() {
		return capacityTextField;
	}

	public JList getRoomList() {
		return roomJList;
	}
	
	public void updateAvailRooms(long start, long end) {
		ReservationMockup askForRooms = new ReservationMockup(-1, start, end);
		askForRooms.setState(Mockup.GET_ROOMS);
		askForRooms.setSessionId(Storage.getSessionID());
		
		//Clears the roomList and model
		roomList.removeAll(roomList);
		model.clear();
		
		try {
			/* when sending askForRoom the client gets a list of available rooms,
			 * and sets the content of the list to the GUI to choose from
			 */
			List<RoomMockup> avail = (List) Storage.getSocket().send(askForRooms);
			for(RoomMockup gotRoom : avail) {
				Room a = (Room)MockupToData.getData(gotRoom);
				roomList.add(a);
				model.addElement(a);
			}		
		} catch (ClassCastException e) {
			System.err.println("AppointmentsRoomDescription::updateAvailRooms: " + e);
	
		} catch (Exception e) {
			System.err.println("AppointmentsRoomDescription::updateAvailRooms: " + e);
		}
	}
	
	class TextFieldKeyListener implements KeyListener{

		@Override
		public void keyPressed(KeyEvent arg0) {

		}

		@Override
		public void keyReleased(KeyEvent arg0) {
			// TODO Auto-generated method stub
			boolean isNumber = true;
			char c;
			String text = getCapacityTextField().getText();
			if(text.length()>0){
				for (int i = 0; i < text.length(); i++) {
					c = text.charAt(i);
					if(!Character.isDigit(c)) isNumber = false;
				}
			} else{
				isNumber = false;
				model.clear();
				for (Room room : roomList) {
					model.addElement(room);
				}
			}
			if(isNumber && text != null){
				int i = Integer.parseInt(text);
				model.clear();
				for (Room room : roomList) {
					if(room.getCapacity() >= i) model.addElement(room);
				}
				roomJList.validate();
				roomJList.updateUI();
			}
		}

		@Override
		public void keyTyped(KeyEvent evt) {

		}

	}
}
